﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

class SteeringHinge : EnhancementBlock
{

    Rigidbody Rigidbody;
    ConfigurableJoint myJoint;
    AnimationCurve animationCurve;

    public override void SafeAwake()
    {
#if DEBUG
        Debug.Log("动力铰链添加摇杆");
#endif
    }

    SteeringWheel steeringWheel;
    public override void OnSimulateStart_EnhancementEnabled()
    {
        Rigidbody = GetComponent<Rigidbody>();
        myJoint = GetComponent<ConfigurableJoint>();
        steeringWheel = GetComponent<SteeringWheel>();

        //if (/*steeringWheel.LimitsSlider.IsActive ||*/ steeringWheel.allowLimits)
        //{
        //    if (steeringWheel.Flipped)
        //    {
        //        animationCurve = new AnimationCurve(new Keyframe[] { new Keyframe(/*-1f**/joyAxis.Min, steeringWheel.LimitsSlider.Max), new Keyframe(0f+joyAxis.Offset, 0f), new Keyframe(1f*joyAxis.Max, -steeringWheel.LimitsSlider.Min) });
        //    }
        //    else
        //    {
        //        animationCurve = new AnimationCurve(new Keyframe[] { new Keyframe(/*-1f * */joyAxis.Min, steeringWheel.LimitsSlider.Min), new Keyframe(0f + joyAxis.Offset, 0f), new Keyframe(1f * joyAxis.Max, -steeringWheel.LimitsSlider.Max) });
        //    }
          
        //}
        //else
        //{
        //   animationCurve = new AnimationCurve(new Keyframe[] { new Keyframe(/*-1f*/joyAxis.Min, -180f), new Keyframe(0f + joyAxis.Offset, 0f), new Keyframe(1f*joyAxis.Max, 180f) });
        //}

        float min = -180f;
        float max = 180f;
        if (steeringWheel.allowLimits)
        {
            if (!steeringWheel.Flipped)
            {
                min = steeringWheel.LimitsSlider.Min;
                max = -steeringWheel.LimitsSlider.Max;
            }
            else
            {
                min = -steeringWheel.LimitsSlider.Min;
                max = steeringWheel.LimitsSlider.Max;
            }
        }
        else
        {
            if (!steeringWheel.Flipped)
            {
                min = 180f;
                max = -180f;
            }
        }

        //animationCurve = new AnimationCurve(new Keyframe[] { new Keyframe(joyAxis.Min, min), new Keyframe(joyAxis.Offset, 0f), new Keyframe(1f * joyAxis.Max, max) });
    }

    public override void SimulateUpdateAlways_EnhancementEnable()
    {
        var value = 0f;
        if (LerpToggle.IsActive)
        {
            value = Mathf.MoveTowards(steeringWheel.AngleToBe,/* animationCurve.Evaluate(*/joyAxis.Value/*)*/, LerpSpeedSlider.Value);
        }
        else
        {
            value = /*animationCurve.Evaluate(*/joyAxis.Value/*)*/;
        }

        steeringWheel.AngleToBe = value;
    }

    public override void SimulateFixedUpdate_EnhancementEnabled()
    {
        if (Rigidbody.IsSleeping())
        {
            Rigidbody.WakeUp();
        }
        try
        {
            if (myJoint.connectedBody.IsSleeping())
            {
                myJoint.connectedBody.WakeUp();
            }
        }
        catch { }
    }
}


